package com.sanshow.charging.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import java.util.Date;
import java.util.List;


public class DbController {

    /**
     * Helper
     */
    private DaoMaster.DevOpenHelper mHelper;//获取Helper对象

    /**
     * DaoMaster
     */
    private DaoMaster mDaoMaster;
    /**
     * DaoSession
     */
    private DaoSession mDaoSession;
    /**
     * 上下文
     */
    private Context context;

    private static DbController mDbController;

    private String dbName = "fb.db";

    /**
     * 获取单例
     */
    public static DbController getInstance(Context context) {
        if (mDbController == null) {
            synchronized (DbController.class) {
                if (mDbController == null) {
                    mDbController = new DbController(context);
                }
            }
        }
        return mDbController;
    }

    /**
     * 初始化
     *
     * @param context
     */
    public DbController(Context context) {
        this.context = context;
        mDaoMaster = new DaoMaster(getWritableDatabase());
        mDaoSession = mDaoMaster.newSession();

        ChargingDao chargingDao = mDaoSession.getChargingDao();
        List<Charging> chargingList = chargingDao.queryBuilder().list();
        if (chargingList == null || chargingList.size() == 0) {
            initChargingData();
            //initWakeboardData();;
        } else {
            updateChargingData(chargingList);
        }

        ChargingMirrorDao chargingMirrorDao = mDaoSession.getChargingMirrorDao();
        List<ChargingMirror> chargingMirrorList = chargingMirrorDao.queryBuilder().list();
        if (chargingMirrorList == null || chargingMirrorList.size() == 0) {
            initChargingMirrorData();
        } else {
            updateChargingMirrorData(chargingMirrorList);
        }

        ChargingDelayDao chargingDelayDao = mDaoSession.getChargingDelayDao();
        List<ChargingDelay> chargingDelayList = chargingDelayDao.queryBuilder().list();
        if (chargingDelayList == null || chargingDelayList.size() == 0) {
            initChargingDelayData();
        } else {
            updateChargingDelayData(chargingDelayList);
        }
    }

    /**
     * 获取可读数据库
     */
    private SQLiteDatabase getReadableDatabase() {
        if (mHelper == null) {
            mHelper = new DaoMaster.DevOpenHelper(context, dbName, null);
        }
        SQLiteDatabase db = mHelper.getReadableDatabase();
        return db;
    }

    /**
     * 获取可写数据库
     *
     * @return
     */
    private SQLiteDatabase getWritableDatabase() {
        if (mHelper == null) {
            mHelper = new DaoMaster.DevOpenHelper(context, dbName, null);
        }
        SQLiteDatabase db = mHelper.getWritableDatabase();
        return db;
    }

    public DaoSession getDaoSession() {
        return mDaoSession;
    }

    private void initChargingData() {
        String itemCode = "";
        ChargingDao chargingDao = mDaoSession.getChargingDao();
        for (int i = 1; i <= 25; i++) {
            if (i < 10) {
                itemCode = "0" + i;
            } else {
                itemCode = String.valueOf(i);
            }

            Charging charging = new Charging();
            charging.setItemCode(itemCode);
            charging.setInfoCode("00000000");
            if (i <= 15) {
                charging.setItemFlag(1);
            } else {
                charging.setItemFlag(2);
            }
            charging.setDoorStatus("00");
            charging.setDianYa(0);
            charging.setDianLiang(0);
            charging.setDianLiu(0);
            charging.setTemperature(0);
            charging.setSmoke(0);
            charging.setCharging(0);
            charging.setLighting(0);
            charging.setIsAvailable(1);
            charging.setExceptInfo("");
            charging.setUpdateTime(new Date());
            charging.setDataInfo("");
            chargingDao.insert(charging);
        }
    }

    private void initChargingMirrorData() {
        String itemCode = "";
        ChargingMirrorDao chargingMirrorDao = mDaoSession.getChargingMirrorDao();
        for (int i = 1; i <= 25; i++) {
            if (i < 10) {
                itemCode = "0" + i;
            } else {
                itemCode = String.valueOf(i);
            }

            ChargingMirror chargingMirror = new ChargingMirror();
            chargingMirror.setItemCode(itemCode);
            chargingMirror.setInfoCode("00000000");
            if (i <= 15) {
                chargingMirror.setItemFlag(1);
            } else {
                chargingMirror.setItemFlag(2);
            }
            chargingMirror.setIsAvailable(1);
            chargingMirror.setDoorStatus("00");
            chargingMirror.setUpdateTime(new Date());
            chargingMirrorDao.insert(chargingMirror);
        }
    }

    private void initChargingDelayData() {
        String itemCode = "";
        ChargingDelayDao chargingDelayDao = mDaoSession.getChargingDelayDao();
        for (int i = 1; i <= 25; i++) {
            if (i < 10) {
                itemCode = "0" + i;
            } else {
                itemCode = String.valueOf(i);
            }

            ChargingDelay chargingDelay = new ChargingDelay();
            chargingDelay.setItemCode(itemCode);
            chargingDelay.setInfoCode("00000000");
            chargingDelay.setItemFlag(0);
            chargingDelay.setUpdateTime(new Date());
            chargingDelayDao.insert(chargingDelay);
        }
    }

    // 开机更新初始化数据
    private void updateChargingData(List<Charging> chargingList) {
        ChargingDao chargingDao = mDaoSession.getChargingDao();
        for (Charging charging : chargingList) {
            charging.setInfoCode("00000000");
            charging.setDoorStatus("00");
            charging.setDianYa(0);
            charging.setDianLiang(0);
            charging.setDianLiu(0);
            charging.setTemperature(0);
            charging.setSmoke(0);
            charging.setCharging(0);
            charging.setLighting(0);
            charging.setExceptInfo("");
            charging.setDataInfo("");
            charging.setUpdateTime(new Date());
            chargingDao.update(charging);
        }
    }

    // 开机更新初始化数据
    private void updateChargingMirrorData(List<ChargingMirror> chargingMirrorList) {
        ChargingMirrorDao chargingMirrorDao = mDaoSession.getChargingMirrorDao();
        for (ChargingMirror chargingMirror : chargingMirrorList) {
            chargingMirror.setInfoCode("00000000");
            chargingMirror.setDoorStatus("01");
            chargingMirror.setUpdateTime(new Date());
            chargingMirrorDao.update(chargingMirror);
        }
    }

    // 开机更新初始化数据
    private void updateChargingDelayData(List<ChargingDelay> chargingDelayList) {
        ChargingDelayDao chargingDelayDao = mDaoSession.getChargingDelayDao();
        for (ChargingDelay chargingDelay : chargingDelayList) {
            chargingDelay.setInfoCode("00000000");
            chargingDelay.setUpdateTime(new Date());
            chargingDelay.setItemFlag(0);
            chargingDelayDao.update(chargingDelay);
        }
    }

    /*private void initWakeboardData() {
        String itemCode = "";
        for (int i = 1; i <= 10; i++) {
            if (i < 10) {
                itemCode = "0" + i;
            } else {
                itemCode = String.valueOf(i);
            }

            WakeboardDao wakeboardDao = mDaoSession.getWakeboardDao();
            Wakeboard wakeboard = new Wakeboard();
            wakeboard.setInfoCode("");
            wakeboard.setItemCode(itemCode);
            wakeboard.setItemState(0);
            wakeboard.setUseState(0);
            wakeboard.setExceptInfo("");
            wakeboard.setUpdateTime(new Date());
            wakeboardDao.insert(wakeboard);
        }
    }*/
}
